package com.filter;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebFilter(filterName = "ThymeleafFilter")
public class ThymeleafFilter implements Filter {
    //Thymeleaf引擎
    private static TemplateEngine engine;
    //前后缀
    private String prefix, suffix;

    @Override
    public void init(FilterConfig config) throws ServletException {
        System.out.println("Thymeleaf引擎初始化。。。");

        //获取配置文件中的thymeleaf前缀
        prefix = config.getInitParameter("thymeleafPrefix");
        if (prefix == null || "".equals(prefix)) {
            prefix = "/WEB-INF/html/";
            System.out.println("....到达网页....");
        }
        //后缀
        suffix = ".html";

        // 实例化 ServletContextTemplateResolver 对象
        ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(config.getServletContext());
        //设置thymeleaf页面前缀
        resolver.setPrefix(prefix);
        //设置thymeleaf页面后缀
        resolver.setSuffix(suffix);
        //设置thymeleaf渲染的编码
        resolver.setCharacterEncoding("UTF-8");
        //设置引擎的渲染模式为HTML
        resolver.setTemplateMode(TemplateMode.HTML);

        //实例化引擎
        engine = new TemplateEngine();
        //设置引擎的参数
        engine.setTemplateResolver(resolver);
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;


        //获取用户请求的url
        String servletPath = request.getServletPath();
        System.out.println("servletPath="+servletPath);
        //如果请求是"/Jump:"开头就直接跳转
        if (servletPath.startsWith("/Jump:")) {
            String jumpPath = servletPath.substring(6);
            if (jumpPath.endsWith(".html")) {
                jumpPath = jumpPath.substring(0, jumpPath.length() - 5);
            }
            request.setAttribute("forward", jumpPath);
            System.out.println("拦截跳转：" + jumpPath + ".html");
        } else {
            //放行
            chain.doFilter(request, response);
        }

        //从request中获取跳转的地址
        Object forward = request.getAttribute("forward");
        if (forward != null) {
            //如果地址存在，转换为字符串
            String forwardUrl = forward.toString();
            //设置输出编码
            response.setCharacterEncoding("utf-8");
            //创建WebContext对象
            WebContext context = new WebContext(request, response, request.getServletContext());
            //渲染页面
            engine.process(forwardUrl, context, response.getWriter());
        }
    }
}
